<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2015, http://www.gooraye.net. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace app\admin\controller;

use app\src\menu\logic\MenuLogic;
use app\src\powersystem\logic\AuthGroupLogic;
use app\src\powersystem\logic\AuthRuleLogic;
use app\src\user\logic\MemberLogic;
use app\src\user\logic\UcenterAdminLogic;
use app\src\user\logic\UcenterMemberLogic;

class AuthManage extends Admin {

	protected function _initialize() {
		parent::_initialize();
	}

	/**
	 * API访问授权	 *
	 */
	public function apiaccess() {

		$map = array();
		$map_access = array();
		$groupid = $this->_param('groupid', -1);
		//模块标识
		$modulename = $this->_param('modulename', '');
		
		$map['status'] = array('egt', 0);
		if ($groupid != -1) {
			$map['group_id'] = $groupid;
		}
		
		$map_access['status'] = 1;
		if (!empty($modulename)) {
			$map_access['module'] = $modulename;
		}
		
		$memberMap = array();
		
		//用户组
//		$result = apiCall(AuthGroupApi::QUERY_NO_PAGING, array($map));
		$result = (new AuthGroupLogic())->queryNoPaging($map);

		//模块
//		$modules = apiCall(AuthRuleApi::ALL_MODULES,array());
		$modules = (new AuthRuleLogic())->allModules();

				
		//访问权限节点
//		$authRules = apiCall(AuthRuleApi::QUERY_NO_PAGING, array($map_access, 'name asc', 'id,module,name,title'));
		$authRules = (new AuthRuleLogic())->queryNoPaging($map_access, 'name asc', 'id,module,name,title');

		if($modules['status']){
			$this->assign("modulename",$modulename);
			$this->assign("modules",$modules['info']);
		}else{
			$this -> error("所属模块数据获取失败！");
		}
		
		if($authRules['status']){
			$this->assign("authrules",$authRules['info']);
		}else{
			$this -> error("规则数据获取失败！");
		}
		if ($result['status']) {
			if ($groupid === -1 && count($result['info']) > 0) {
				//默认第一个用户组
				$groupid = $result['info'][0]['id'];
				$rules = $result['info'][0]['rules'];
			} else {
				$rules = $this -> getRules($result['info'], $groupid);
			}
			//当前用户组拥有的规则
			$this -> assign("rules", $rules);
			//当前用户组
			$this -> assign("groupid", $groupid);
			$this -> assign("groups", $result['info']);
			
			return  $this -> boye_display();;
		} else {
			$this -> error($result['info']);
		}
	}

	/**
	 * 成员授权
	 *
	 */
	public function user() {
		$map = array();
		$groupid = $this->_param('groupid', -1);
        $param = [];
		$map['status'] = array('egt', 0);
		if ($groupid != -1) {
			$map['group_id'] = $groupid;
			$param['groupid'] = $groupid;
		}
		$memberMap = array();

		//用户组
		$result = (new AuthGroupLogic())->queryNoPaging([]);
		if ($result['status']) {
			if ($groupid === -1) {
				$groupid = $result['info'][0]['id'];
			}

			$this -> assign("groupid", $groupid);
			$this -> assign("groups", $result['info']);

			$memberMap['status'] = array('egt', 0);
			$memberMap['group_id'] = $groupid;
			//查询用户信息
			$result = (new MemberLogic())->queryByGroup($memberMap, array('curpage' => $this->_param('p', 0), 'size' => 10),$param);

			if ($result['status']) {
				if(!empty($result['info']['list'])){
					foreach($result['info']['list'] as $k=>$v){
						$tem[$k]=$v;
						$user_id=(new UcenterAdminLogic())->getInfo(['member_id'=>$v['uid']]);

						if(!empty($user_id['info'])){
							$mem=(new MemberLogic())->getInfo(['uid'=>$user_id['info']['uid']]);
							$ucen=(new UcenterMemberLogic())->getInfo(['id'=>$user_id['info']['uid']]);
							$tem[$k]['user']=['nickname'=>$mem['info']['nickname'],'mobile'=>$ucen['info']['mobile']];
						}else{
							$tem[$k]['user']=['nickname'=>'未绑定','mobile'=>'未绑定'];
						}
					}

				}else{
					$tem=[];
				}
				$this -> assign("show", $result['info']['show']);
				$this -> assign("list", $tem);
			}
			return  $this -> boye_display();;
		} else {
			$this -> error($result['info']);
		}
	}

	/*
	 * 绑定用户
	 */
	public function binding(){
		$admin_uid=input('uid');
		$group_id=input('groupid');
		if(empty($admin_uid))$this->error('找不到管理员');
		$this->assign('uid',$admin_uid);

		$this->assign('groupid',$group_id);
		return $this->boye_display();
	}
	public function new_binding(){
		$admin_id=input('admin_id');
		$user_id=input('uid');
		$group_id=input('group_id');

		if(empty($admin_id)||empty($user_id)) $this->error('缺少数据');
		$ucenter_admin=(new UcenterAdminLogic())->getInfo(['member_id'=>$admin_id]);
		if(!empty($ucenter_admin['info'])){
			$save=(new UcenterAdminLogic())->save(['id'=>$ucenter_admin['info']['id']],['uid'=>$user_id]);
		}else{
			$add=(new UcenterAdminLogic())->add(['member_id'=>$admin_id,'uid'=>$user_id,'status'=>1],'');
		}

		if($save||$add){
			$this->success('绑定成功','/admin.php/admin/auth_manage/user/groupid/'.$group_id);
		}else{
			$this->error('绑定失败');
		}

	}

	/**
	 * 访问授权	 *
	 */
	public function access() {

		$map = array();
		$map_access = array();

		$groupid = $this->_param('groupid', -1);
		//模块标识
		$modulename = $this->_param('modulename', '');

		$map['status'] = array('egt', 0);
		if ($groupid != -1) {
			$map['id'] = $groupid;
		}
//		$result = apiCall("Admin/AuthGroup/queryNoPaging", array($map));
        $result = (new AuthGroupLogic())->queryNoPaging($map);

		$map_access['status'] = 1;
		if (!empty($modulename)) {
			$map_access['module'] = $modulename;
		}


		//菜单列表
//		$menulist = apiCall("Admin/Menu/queryNoPaging", array($map_access, 'pid asc', 'id,title,url,pid,group'));
		$menulist = (new MenuLogic())->queryNoPaging($map_access, 'pid asc', 'id,title,url,pid,group');

		if ($menulist['status']) {
			$menus = "";
			//获取当前用户组的菜单列表
			if (!isset($map['id'])) {
				$map['id'] = 1;
			}
//			$menus = apiCall("Admin/AuthGroup/getInfo", array($map));
			$menus = (new AuthGroupLogic())->getInfo($map);


			if ($menus['status']) {
				$menus = $menus['info']['menulist'];
			}
			$tree = list_to_tree($menulist['info']);

			$tree = $this -> createTree($tree, $menus);

			$this -> assign("tree", $tree['child']);
		} else {
			$this -> error('获取数据失败！');
		}

		if ($result['status']) {
			if ($groupid === -1 && count($result['info']) > 0) {
				//默认第一个用户组
				$groupid = $result['info'][0]['id'];
				$rules = $result['info'][0]['rules'];
			} else {
				$rules = $this -> getRules($result['info'], $groupid);
			}
			//当前用户组拥有的规则
			$this -> assign("rules", $rules);
			//当前用户组
			$this -> assign("groupid", $groupid);
			$this -> assign("groups", $result['info']);

			return  $this -> boye_display();;
		} else {
			$this -> error($result['info']);
		}
	}

	/**
	 * 根据用户组id，获取用户组对应规则
	 * @param $groups 用户组列表
	 * @param $groupid 用户组id
	 * @return string 用户组规则
	 */
	private function getRules($groups, $groupid) {

		foreach ($groups as $vo) {
			if ($vo['id'] == $groupid) {
				return $vo['rules'];
			}
		}
	}

    /**
	 * 根据菜单列表生成树形数据
	 * @param $tree 所有的菜单数据
	 * @param $menus 当前用户组的所对应的菜单，字符串
	 */
	private function createTree($tree, $menus) {
		$allchecked	= true;//所有子菜单都选中了
		$ul = "<ul>";
		foreach ($tree as $vo) {
			$ul .= "<li ";
			$childUL['allchecked'] = true;
			if (isset($vo['_child'])) {
				$childUL = $this -> createTree($vo['_child'],$menus);
			}
			
			if (strpos($menus, $vo['id'] . ",") === false || !$childUL['allchecked']	) {
				$allchecked = false;
				$selected = 'false';
			} else {
				
				$selected = 'true';
			}
			
			if ($vo['pid'] == 0) {
				$jstree = '{ "selected" : ' . $selected . ', "opened" :  true}';
			} else {
				$jstree = '{ "selected" : ' . $selected . ', "opened" : false }';
			}
			$ul .= "data-jstree='".$jstree."' id=\"jstree_" . $vo['id'] . "\" >" . $vo['title'];

            if (isset($childUL['child'])) {
                $ul .= $childUL['child'];
            }
			$ul .= "</li>";
		}

		$ul .= "</ul>";
		return array('child'=>$ul,'allchecked'=>$allchecked);
	}

	

	//$VO必是有Admin/Index/index 的字符串
	private function getCTRLName($vo) {
		$temp = explode('/', $vo);
		if (count($temp) == 3) {
			return $temp[0] . '_' . $temp[1];
		} else {
			return "errURL";
		}
	}

}
